/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.model;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.gwt.user.client.ui.HasName;

import javax.jdo.annotations.*;
import java.util.List;

/**
 *
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class ThreadInformation extends ModelObject implements HasName {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;

    @Persistent
    private Failure failure;

    @Persistent
    private String name;

    @Persistent(mappedBy = "thread")
    @Order(extensions = @Extension(vendorName="datanucleus", key="list-ordering", value="index asc"))
    private List<StackTraceElement> stackTrace;

    @Persistent(mappedBy = "thread")
    @Order(extensions = @Extension(vendorName="datanucleus", key="list-ordering", value="index asc"))
    private List<LoggingEvent> loggingEventList;

    public ThreadInformation() {
    }

    public ThreadInformation(String name) {
        this.name = name;
    }

    public Key getId() {
        return id;
    }

    public Failure getFailure() {
        return failure;
    }

    public void setFailure(Failure failure) {
        this.failure = failure;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void addStackTraceElement(StackTraceElement stackTraceElement) {
        stackTrace.add(stackTraceElement);
    }

    public List<StackTraceElement> getStackTrace() {
        return stackTrace;
    }

    public void setStackTrace(List<StackTraceElement> stackTrace) {
        this.stackTrace = stackTrace;
    }

    public void addLoggingEvent(LoggingEvent loggingEvent) {
        loggingEventList.add(loggingEvent);
    }
    
    public List<LoggingEvent> getLoggingEventList() {
        return loggingEventList;
    }

    public void setLoggingEventList(List<LoggingEvent> loggingEventList) {
        this.loggingEventList = loggingEventList;
    }

    public void delete() {
        //Delete all logging events
        deleteAll(loggingEventList);
        //Delete all stack trace elements
        deleteAll(stackTrace);
        //Delete self
        super.delete();
    }

    public String keyToString() {
        return keyToString(id);
    }

    public static String keyToString(Key id) {
        return Long.toString(id.getId());
    }

    public static Key stringToKey(Key failureId, String id) {
        return KeyFactory.createKey(failureId, ThreadInformation.class.getSimpleName(), Long.parseLong(id));
    }
}
